<template>
	<view class="password-login">
		<u-toast ref="uToast" />

		<view class="password-login__form">
			<u-form>
				<u-form-item left-icon="account" prop="name">
					<u-input v-model="phone" placeholder="请输入用户名" />
				</u-form-item>
				<u-form-item left-icon="lock" prop="name">
					<u-input type="password" :clearable="false" v-model="psd" placeholder="请输入密码" />
				</u-form-item>
			</u-form>
			<u-button class="mt-36" type="primary" @click="submit">登录</u-button>
			<view class="mt-36 flex align-center justify-between">
				<text class="password-login__tip price-text"></text>
				<text class="password-login__tip" @click.stop="forgotPassword">忘记密码</text>
			</view>
		</view>
		<!-- <view class="flex justify-center password-login__footer">
			<text>登录即同意</text>
			<text class="password-login__footer--link" @click="onPrivacy">《用户隐私协议》</text>
		</view> -->
	</view>
</template>


<script>
export default {
	data() {
		return {
			psd: null,
			phone: null,
			loginForm: {
				loginType: "AGENT_APP",
				phone: null,
				username: null,
				cypherKey: null,
			},
		};
	},
	methods: {
		onPrivacy() {
			uni.navigateTo({
				url: "/pages/main/privacy/privacy",
			});
		},
		forgotPassword() {
			uni.navigateTo({
				url: "/pages/main/forgot-password/forgot-password",
			});
		},
		handleChangeType() {
			this.$emit("change", "code");
		},
		submit() {
			if (this.phone === null) {
				this.$zx.msg("手机号不能为空");
				return;
			}
			if (this.psd === null) {
				this.$zx.msg("密码不能为空");
				return;
			}
			let psd = this.psd;
			this.loginForm.username = this.phone;
			this.loginForm.phone = this.phone;
			// 这个就是rsa加密后的值
			this.loginForm.cypherKey = this.$zx.RSAencrypt(psd)

			this.$zx.user_api.phoneLogin(this.loginForm).then((data) => {
				console.log("data=" + JSON.stringify(data))
				uni.setStorageSync("token", data.loginToken);
				uni.setStorageSync("user_info", data.agentUser);
				uni.setStorageSync("agentNo", data.currentIdentity.agentNo);
				data.userType = "staff";

				if (data.userType === "staff") {
					// 获取权限列表
					this.$zx.user_api.getUserInfo().then((info) => {
						uni.setStorageSync("permissions", info.permissions);
						uni.setStorageSync("user_info", info);
					});
				}

				uni.setStorageSync("login_role", data.userType);
				if (getCurrentPages().length === 1) {
					if (data.userType === "staff") {
						uni.reLaunch({
							url: "/pages/staff-home/staff-home",
						});
					}
					if (data.userType === "worker") {
						uni.reLaunch({
							url: "/pages/worker-home/worker-home",
						});
					}
				} else {
					uni.navigateBack({
						delta: 1,
					});
				}
			});
		},
	},
};
</script>
<style scoped lang="scss">
.password-login {
	height: 100%;
	position: relative;



	&__form {
		// width: 700rpx;
		// height: 400rpx;
		// background-color: #ffffff;
		border-radius: 24rpx;
		margin: 0 auto;
		position: relative;
		// top: 126rpx;
		z-index: 11;
		padding: 28rpx;
		padding-top: 66rpx;
	}

	&__tip {
		color: #9D60FF;
		font-size: 26rpx;
	}

	&__footer {
		// position: absolute;
		font-size: 24rpx;
		// bottom: 50rpx;
		// left: 220rpx;

		&--link {
			color: #9D60FF;
		}
	}
}
</style>